//log 10
Math.log10 = function(x) {
	return Math.log(x) / Math.log(10);
};
// Math float calculate ===============================
Math.flen = function(n) {
	var l = 0;
	var s = String(n);
	var a = s.split(".");
	if (a.length > 1) {
		l = a[1].length;
	}
	return l;
};
// +
Math.add = function(n1, n2) {
	var r1 = Math.flen(n1);
	var r2 = Math.flen(n2);
	if (r1 + r2 == 0) {
		return n1 + n2;
	} else {
		var m = Math.pow(10, Math.max(r1, r2));
		return (n1 * m + n2 * m) / m;
	}
};
// -
Math.sub = function(n1, n2) {
	var r1 = Math.flen(n1);
	var r2 = Math.flen(n2);
	if (r1 + r2 == 0) {
		return n1 - n2;
	} else {
		var m = Math.pow(10, Math.max(r1, r2));
		var n = (r1 >= r2) ? r1 : r2;
		return ((n1 * m - n2 * m) / m).toFixed(n);
	}
};
// *
Math.mul = function(n1, n2) {
	var r1 = Math.flen(n1);
	var r2 = Math.flen(n2);
	if (r1 + r2 == 0) {
		return n1 * n2;
	} else {
		var m1 = parseInt(String(n1).replace(".", ""));
		var m2 = parseInt(String(n2).replace(".", ""));
		return (m1 * m2) / Math.pow(10, r1 + r2);
	}
};
// /
Math.div = function(n1, n2) {
	var r1 = Math.flen(n1);
	var r2 = Math.flen(n2);
	if (r1 + r2 == 0) {
		return n1 / n2;
	} else {
		var m1 = parseInt(String(n1).replace(".", ""));
		var m2 = parseInt(String(n2).replace(".", ""));
		return (m1 / m2) * Math.pow(10, r2 - r1);
	}
};
// clamp with 2 numbers
Math.clamp = function(num, min, max) {
	return Math.max(Math.min(num, max), min);
};
// get a nice number list for graph labels
// return a array
Math.nicenum = function(min, max, num) {
	// http://gurge.com/blog/2008/04/02/google-chart-tips-for-ruby-hackers/
	// http://wiki.tcl.tk/9503
	// http://mustafashaik.wordpress.com/2010/11/20/nice-numbers-for-graph-labels/
	function nice(x, round) {
		var expv = Math.floor(Math.log(x) / Math.log(10));
		var f = x / Math.pow(10, expv);
		var nf;
		if (round) {
			if (f < 1.5) {
				nf = 1;
			} else if (f < 3) {
				nf = 2;
			} else if (f < 7) {
				nf = 5;
			} else {
				nf = 10;
			}
		} else {
			if (f <= 1) {
				nf = 1;
			} else if (f <= 2) {
				nf = 2;
			} else if (f <= 5) {
				nf = 5;
			} else {
				nf = 10;
			}
		}
		return nf * Math.pow(10, expv);
	}

	function loose(min, max, num) {
		num = num || 4;
		if (min == max) {
			max = min + 1;
		} else if (min > max) {
			var n = min;
			min = max;
			max = n;
		}
		var range = nice(max - min, false);
		var d = nice(range / (num - 1), true);
		return {
			min : Math.floor(min / d) * d,
			max : Math.ceil(max / d) * d,
			val : d
		}
	}

	var obj = loose(min, max, num);
	var arr = [];
	var v = obj.min;
	while (v <= obj.max) {
		arr.push(v);
		v = Math.add(v, obj.val);
	}
	return arr;

};
